Class WinServices
	Private objWmi
	Private blnConnect

	Private Sub Class_Initialize()
		Set objWmi = New Wmi
		
		blnConnect = False
		objWmi.WmiPath = "\root\cimv2"
	End Sub
	
	Public Property Let Login(ByVal varValue)
		objWmi.Login = varValue
	End Property
	
	Public Property Let Password(ByVal varValue)
		objWmi.Password = varValue
	End Property
	
	Public Property Let IpAddress(ByVal varValue)
		objWmi.IpAddress = varValue
	End Property
	
	Public Sub Connect()
		objWmi.Connect()
		
		blnConnect = True
	End Sub
	
	Public Function ServiceState(ByVal strServiceName)
		If Not blnConnect Then
			Err.Raise 5000, "Services->StopService", "Error Description"
		End If
	
		Set colServices = objWmi.WmiService.ExecQuery("Select * From Win32_Service Where DisplayName = '" & strServiceName & "'")
	
		If colServices.Count = 0 Then
			Err.Raise 5000, "Services->ServiceState", "Error Description"
		
			Exit Function
		End If
		
		For Each objService in colServices
			ServiceState = objService.State
			
			Exit Function
		Next
	End Function
	
	Public Sub StartService(ByVal strServiceName)
		If Not blnConnect Then
			Err.Raise 5000, "Services->StopService", "Error Description"
		End If
	
		Set colServices = objWmi.WmiService.ExecQuery("Select * From Win32_Service Where DisplayName = '" & strServiceName & "'")

		If colServices.Count = 0 Then
			Err.Raise 5000, "Services->StartService", "Error Description"
		
			Exit Sub
		End If
		
		For Each objService in colServices
			If UCase(objService.State) = "STOPPED" Then
				objService.StartService()
				
				Exit For
			End If
		Next

		intCycles = 70
		blnServiceRun = False
		
		Do While intCycles > 0
			If UCase(ServiceState(strServiceName)) = "RUNNING" Then
				blnServiceRun = True
			
				Exit Do
			End If
		
			intCycles = intCycles-1
		
			WScript.Sleep 1000
		Loop
		
		If Not blnServiceRun Then Err.Raise 5000, "Service->StartService", "Error Description"
	End Sub
	
	Public Sub StopService(ByVal strServiceName)
		If Not blnConnect Then
			Err.Raise 5000, "Services->StopService", "Error Description"
		End If
	
		Set colServices = objWmi.WmiService.ExecQuery("Select * From Win32_Service Where DisplayName = '" & strServiceName & "'")

		If colServices.Count = 0 Then
			Err.Raise 5000, "Services->StopService", "Error Description"
		
			Exit Sub
		End If
		
		For Each objService in colServices
			If UCase(objService.State) = "RUNNING" Then
				objService.StopService()
				
				Exit For
			End If
		Next

		intCycles = 70
		blnServiceStopped = False
		
		Do While intCycles > 0
			If UCase(ServiceState(strServiceName)) = "STOPPED" Then
				blnServiceStopped = True
			
				Exit Do
			End If
		
			intCycles = intCycles-1
		
			WScript.Sleep 1000
		Loop
		
		If Not blnServiceStopped Then Err.Raise 5000, "Service->StopService", "Error Description"
	End Sub
End Class